package com.scqkxx.gamebox.activity.home;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.home.adapter.HomeNewsAdapter;
import com.scqkxx.gamebox.activity.home.adapter.NavSpaceAdapter;
import com.scqkxx.gamebox.activity.home.adapter.NavTeamAdapter;
import com.scqkxx.gamebox.activity.myself.MySelfSettingActivity;
import com.scqkxx.gamebox.activity.space.RecycleActivity;
import com.scqkxx.gamebox.activity.space.SearchActivity;
import com.scqkxx.gamebox.activity.news.NewsActivity;
import com.scqkxx.gamebox.common.Const;
import com.scqkxx.gamebox.databinding.NavBinding;
import com.scqkzqtz.base.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 * Created by hef on 2018/8/10.
 */

public class NavigationFragment extends BaseFragment<NavBinding> implements View.OnClickListener {
    private List<AVObject> listNews = new ArrayList<>();
    private HomeNewsAdapter homeNewsAdapter;

    private NavSpaceAdapter navSpaceAdapter;
    private NavTeamAdapter navTeamAdapter;
    private List<AVObject> spaceList = new ArrayList<>();
    private List<AVObject> teamList = new ArrayList<>();

    @Override
    public void loadData() {

    }

    @Override
    public int setLayoutId() {
        return R.layout.nav;
    }

    @Override
    public void init() {
        initView();
        initData();
    }

    private void initView() {
        binding.board.setOnClickListener((View view) -> {
            EventBus.getDefault().post(Const.CLOSE_NAV);
        });
        binding.space.setOnClickListener((View view) -> {
            binding.rvSpaceList.setVisibility(binding.rvSpaceList.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        });
        binding.team.setOnClickListener((View view) -> {
            binding.rvTeamList.setVisibility(binding.rvTeamList.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
        });
        binding.object.setOnClickListener((View view) -> {
            startActivity(new Intent(this._Activity, MySelfSettingActivity.class));
            EventBus.getDefault().post(Const.CLOSE_NAV);
        });
        binding.fly.setOnClickListener((View view) -> {
            startActivity(new Intent(this._Activity, NewsActivity.class));
            EventBus.getDefault().post(Const.CLOSE_NAV);
        });
        binding.recycle.setOnClickListener((View view) -> {
            startActivity(new Intent(this._Activity, RecycleActivity.class));
            EventBus.getDefault().post(Const.CLOSE_NAV);
        });
        binding.search.setOnClickListener((View view) -> {
            startActivity(new Intent(this._Activity, SearchActivity.class));
            EventBus.getDefault().post(Const.CLOSE_NAV);
        });
        binding.ivHelp.setOnClickListener((View view) -> {
            EventBus.getDefault().post(Const.CLOSE_NAV);
        });
    }

    public void hideItemList() {
        if (binding == null) return;
        binding.rvSpaceList.setVisibility(View.GONE);
        binding.rvTeamList.setVisibility(View.GONE);
    }

    @Override
    public void onResume() {
        super.onResume();
        AVQuery<AVObject> spaceQuery = AVQuery.getQuery("Space");
        spaceQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                if (avException == null) {
                    spaceList.clear();
                    spaceList.addAll(avObjects);
                    navSpaceAdapter.notifyDataSetChanged();
                }
            }
        });

        AVQuery<AVObject> newsQuery = AVQuery.getQuery("News");
        newsQuery.whereEqualTo("belongGroupId",AVUser.getCurrentUser().getInt("defaultGroupId"));
        newsQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> avObjects, AVException avException) {
                if(avException == null){
                    binding.tvNewsCount.setVisibility(avObjects.size() > 0 ? View.VISIBLE : View.GONE);
                    binding.tvNewsCount.setText(avObjects.size() + "");
                }
            }
        });
    }

    private void initData() {
        navSpaceAdapter = new NavSpaceAdapter(R.layout.item_nav_space, spaceList);
        navTeamAdapter = new NavTeamAdapter(R.layout.item_nav_team, teamList);
        binding.rvSpaceList.setLayoutManager(new LinearLayoutManager(_Activity, LinearLayoutManager.VERTICAL, false));
        binding.rvTeamList.setLayoutManager(new LinearLayoutManager(_Activity, LinearLayoutManager.VERTICAL, false));
        binding.rvSpaceList.setAdapter(navSpaceAdapter);
        binding.rvTeamList.setAdapter(navTeamAdapter);

        teamList.clear();
        AVObject avObject1 = new AVObject();
        AVObject avObject2 = new AVObject();
        AVObject avObject3 = new AVObject();
        avObject1.put("name", "我的群组");
        avObject2.put("name", "邀请加入");
        avObject3.put("name", "加入群组");
        teamList.add(avObject1);
        teamList.add(avObject2);
        teamList.add(avObject3);
        navTeamAdapter.notifyDataSetChanged();
//        AVQuery<AVObject> teamQuery = AVQuery.getQuery("Group");
//        teamQuery.findInBackground(new FindCallback<AVObject>() {
//            @Override
//            public void done(List<AVObject> avObjects, AVException avException) {
//                if (avException == null) {
//                    teamList.clear();
//                    teamList.addAll(avObjects);
//                    navTeamAdapter.notifyDataSetChanged();
//                }
//            }
//        });

    }


    @Override
    public void onClick(View v) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
